require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _address = require('service/address-service.js');
var _order = require('service/order-service.js');
var _mm = require('util/mm.js');
var templateAddress = require('./address.string');
var templateProduct = require('./product.string');
var addressModel = require('./address-model.js');

var page = {
    data : {
      selectedAddressId : null  
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadAddressList();
        this.loadProductList();
    },
    bindEvent: function () {
        var _this = this;
        //添加地址
        $(document).on('click','.address-add',function(){
            addressModel.show({
                isUpdate : false,
                onSuccess : function(){
                    _this.loadAddressList();
                }
            });
        });
//        地址的编辑
        $(document).on('click','.address-update',function(e){
            e.stopPropagation();
            var shippingId = $(this).parents('.address-item').data('id');
            _address.getAddress(shippingId,function(res){
                addressModel.show({
                    isUpdate : true,
                    data : res,
                    onSuccess : function(){
                        _this.loadAddressList();
                    }
                });
            },function(errMsg){
                _mm.errorTips(errMsg);
            });
        });
//        删除地址 
        $(document).on('click','.address-delete',function(e){
            e.stopPropagation();
            var id = $(this).parents('.address-item').data('id');
            if(window.confirm('确定删除该地址？')){
                _address.deleteAddress(id,function(res){
                    _this.loadAddressList();
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }
        });
//        地址的选择
        $(document).on('click','.address-item',function(){
            $(this).addClass('active').siblings('.address-item').removeClass('active');
            _this.data.selectedAddressId = $(this).data('id');
        });
//        订单的提交
        $(document).on('click','.order-submit',function(){
            var shippingId = _this.data.selectedAddressId;
            if(shippingId){
                _order.createOrder({
                    shippingId : shippingId
                },function(res){
                    window.location.href = './payment.html?orderNumber='+res.orderNo;
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }else{
                _mm.errorTips('请选择地址后再提交');
            }
        })
    },
    //加载地址列表
    loadAddressList : function(){
        var _this = this;
        $('.address-con').html('<div class="loading"></div>');
        _address.getAddressList(function(res){
            _this.addressFilter(res);
            var addressListHtml = _mm.renderHtml(templateAddress,res);
            $('.address-con').html(addressListHtml);
        },function(errMsg){
            $('.address-con').html('<p class="err-tip">地址加载失败，请刷新后重试</p>');
        });
    },
    //加载商品清单
    loadProductList : function(){
        var _this = this;
        $('.order-product').html('<div class="loading"></div>');
        //获取订单列表
        _order.getProductList(function(res){
            var productListHtml = _mm.renderHtml(templateProduct,res);
            $('.order-product').html(productListHtml);
        },function(errMsg){
            $('.order-product').html('<p class="err-tip">商品信息加载失败，请刷新后重试</p>');
        });
    },
    //处理地址列表中的选中状态
    addressFilter : function(data){
        if(this.data.selectedAddressId){
            var selectAddressFlag = false;
            for(var i=0,length=data.list.length;i<length;i++){
                if(data.list[i].id === this.data.selectedAddressId){
                    data.list[i].isActive = true;
                    selectAddressFlag = true;
                }
            }
            if(!selectAddressFlag){
                this.data.selectedAddressId = null;
            }
        }
    }
};
$(function () {
    page.init();
});
